<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { numberApi } from '@/api/controller'
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    dialogFormVisible:false,
    title:''
  })
  const form = reactive({
    user_id:'',
    phone:''
  })
  const formRules = {
    
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(valid) {
        const loading = Toastloading()
        await numberApi.changeBindPhone(form)
        ToastSuccess('修改成功')
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = (query:any = {})=> {
    const { user_id='', nickname='', mobile_phone='' } = query;
    Object.assign(form,{
      user_id,
      phone:mobile_phone,
    })
    action.title = `${nickname}换绑手机号`
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    Object.assign(form,{
      user_id:'',
      phone:'',
    })
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
    top="10vh"
    class="xm-dialog"
    v-model="action.dialogFormVisible"
    :close-on-click-modal="false"
    :show-close="false"
    :close-on-press-escape="false"
    :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules" :label-width="formLabelWidth">
        <el-form-item label="手机号" prop="nickname" >
          <el-input v-model="form.phone" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit()">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped >
  .xm-dialog{
    .el-input {
        width: calc($inputWidth);
    }
}
</style>